/*
 *  This file is part of Social Media Monitoring Toolbox.
 *
 *  Social Media Monitoring Toolbox is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Social Media Monitoring Toolbox is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Social Media Monitoring Toolbox. If not, see <http://www.gnu.org/licenses/lgpl.html>.
 *
 * � Copyright 2013
 * Jana Asmussen, Julian Bau�, Daniela Dalock, Christian Gutjahr,
 * Fabian Heidorn, Alexander Kaack, Vitali Kagadij 
 * 
 */

package gui;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.event.ActionEvent;

import org.primefaces.model.LazyDataModel;

import types.StreamDescriptor;
import types.TableTyp;
import types.TweetPost;
import types.YoutubeVideoType;

@ManagedBean(name = "listAllBean")
@ViewScoped
public class ListAll implements Serializable {

	private static final long serialVersionUID = 7293485588445006702L;
	private List<TableTyp> all;
	private GetData data = new GetData();
	private String selectedOption = "youtube";
	private LazyDataModel<TableTyp> lazyModel;

	public ListAll() {
		createList(selectedOption);
	}

	/**
	 * Create the lazy list for all entries of the param
	 * 
	 * @param type
	 *            for the listing - youtube of twitter
	 */
	private void createList(String type) {
		this.all = null;
		this.all = new ArrayList<TableTyp>();
		int count = 1;
		if (type.equals("twitter")) {
			List<StreamDescriptor> streams = data.getTwitterStreams();			
			for (StreamDescriptor streamDescriptor : streams) {
				List<TweetPost> tweets = data.getTweets(streamDescriptor, null);
				
				for (TweetPost tweetPost : tweets) {
					
					// if(!this.all.contains(tweetPost.getTweetText())){
					TableTyp tabletyp = new TableTyp();
					// tabletyp.setGeoTag(tweetPost.getGeoTag().toString());
					int sentiment = tweetPost.getSentiment();
					String sentimentText = "";
					if (sentiment == 0) {
						sentimentText = "neutral";
					} else if (sentiment == 1) {
						sentimentText = "positive";
					} else if (sentiment == 2) {
						sentimentText = "negative";
					}
					tabletyp.setId(count++);
					tabletyp.setSentiment(sentimentText);
					tabletyp.setName(tweetPost.getTweetText());
					this.all.add(tabletyp);
					// }

				}
			}
		} else if (type.equals("youtube")) {
			List<StreamDescriptor> streams = data.getYoutubeStreams();
			for (StreamDescriptor streamDescriptor : streams) {
				List<YoutubeVideoType> clips = data.getYouTubeClips(
						streamDescriptor, null);
				for (YoutubeVideoType clip : clips) {
					// if(!this.all.contains(clip.getVideoTitle())){
					TableTyp tabletyp = new TableTyp();
					// tabletyp.setGeoTag(Double.toString(clip.getGeoTag().getLatitude())+Double.toString(clip.getGeoTag().getLongitude()));
					int likes = clip.getLikes();
					int dislikes = clip.getDislikes();
					String sentimentText = "";
					if (likes == dislikes) {
						sentimentText = "neutral";
					} else if (likes > dislikes) {
						sentimentText = "positive";
					} else if (likes < dislikes) {
						sentimentText = "negative";
					}
					tabletyp.setId(count++);
					tabletyp.setSentiment(sentimentText);
					tabletyp.setName(clip.getVideoTitle());
					this.all.add(tabletyp);
					// }
				}
			}
		}
		updateLazyModelAll();
	}

	/**
	 * Update the Lazylist
	 * 
	 * @param ae
	 *            ActionEvent, that triggers this method
	 * @param id
	 *            id of the stream
	 */
	public void updateAllList(ActionEvent ae, String id) {
		System.out.println("ID!!!!!!!!! " + id);
		createList(id);
	}

	/**
	 * Update the lazymodel with the existing list of posts
	 */
	public void updateLazyModelAll() {
		lazyModel = null;
		lazyModel = new Lazy(this.all);
	}

	/**
	 * Getter for the lazymodule
	 * 
	 * @return the existing lazymodule
	 */
	public LazyDataModel<TableTyp> getLazyModel() {
		return lazyModel;
	}

	/**
	 * Getter for the post list
	 * 
	 * @return the list of all posts
	 */
	public List<TableTyp> getAll() {
		return all;
	}

	/**
	 * Setter for the post list
	 * 
	 * @param set
	 *            the list of all posts
	 */
	public void setAll(List<TableTyp> all) {
		this.all = all;
	}

	/**
	 * Getter for the selected option
	 * 
	 * @return selected option - twitter or youtube
	 */
	public String getSelectedOption() {
		return selectedOption;
	}

	/**
	 * Setter for the selected option
	 * 
	 * @param selectedOption
	 *            - twitter or youtube
	 */
	public void setSelectedOption(String selectedOption) {
		this.selectedOption = selectedOption;
	}
}
